package com.zhudoo.zhilian.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhudoo.zhilian.dao.entity.City;
import com.zhudoo.zhilian.dao.entity.CityLine;
import com.zhudoo.zhilian.dao.entity.CityStation;
import com.zhudoo.zhilian.dao.impl.CityDao;
import com.zhudoo.zhilian.util.web.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhudao
 */
@Service
public class CityService {

    @Autowired
    CityDao cityDao;

    public Object getCityList() {
        List<City> cities = cityDao.getCityList();
        return cities;
    }

    public Object getLineList(ServiceContext serviceContext) {
        List<Map<String, Object>> citySubwayArray = new ArrayList<>();
        int cityId = serviceContext.getParamsInteger("cityId");
        List<CityLine> cityLines = cityDao.getCityLineByCityId(cityId);
        JSONObject citySubwayDefault = new JSONObject();
        citySubwayDefault.put("name", "不限");
        citySubwayDefault.put("value", "0");

        JSONObject cityStationDefault = new JSONObject();
        cityStationDefault.put("name", "不限");
        cityStationDefault.put("value", "0");

        JSONArray childDefault = new JSONArray();
        childDefault.add(cityStationDefault);
        citySubwayDefault.put("child", childDefault);
        citySubwayArray.add(citySubwayDefault);

        for (CityLine cityLine : cityLines) {
            JSONObject citySubwayJson = new JSONObject();
            citySubwayJson.put("name", cityLine.getName());
            citySubwayJson.put("value", cityLine.getId());
            List<CityStation> cityStationList = cityDao.getCityStationBylineId(cityLine.getId());
            JSONArray child = new JSONArray();
            child.add(cityStationDefault);
            for (CityStation cityStation : cityStationList) {
                JSONObject cityStationJson = new JSONObject();
                cityStationJson.put("name", cityStation.getName());
                cityStationJson.put("value", cityStation.getId());
                child.add(cityStationJson);
            }
            citySubwayJson.put("child", child);
            citySubwayArray.add(citySubwayJson);
        }
        return citySubwayArray;
    }
}
